package cn.edu.jxust.controller.api.sysresource;

import cn.edu.jxust.common.domain.entity.Account;
import cn.edu.jxust.common.domain.entity.Role;
import cn.edu.jxust.common.domain.dto.RoleResourceVo;
import cn.edu.jxust.common.domain.dto.RoleVo;
import cn.edu.jxust.common.domain.web.api.sysresource.IRoleController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.RoleResourceService;
import cn.edu.jxust.core.domain.repository.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouzhicheng
 * @since 2020-06-29
 */
@Slf4j
@RestController
public class  RoleController implements IRoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private RoleResourceService roleResourceService;

    @Override
    public Result getOneRole(Integer id){

        try{
            Role role = roleService.getById(id);
            if(role != null){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, role);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "没有该角色任何信息");
            }
        }catch (Exception e){
            log.error("RoleController --> getOneRole: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getRoleList(){

        try{
            //获取当前 roleId
            Integer roleId = ((Account) SecurityUtils.getSubject().getPrincipal()).getRoleId();
            List<Role> roles = roleService.getRoles(roleId);
            if(roles != null && !roles.isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, roles);
            }else{
                return ResultUtils.resultWithoutData(ResultStatus.FAILED, "未找到任何角色信息");
            }
        }catch (Exception e){
            log.error("RoleController --> getRoleList: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }


    }

    @Override
    public Result updateRole(RoleVo roleVo, BindingResult bing){

        if(bing.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return roleService.modifyRoleInfo(roleVo);
        }catch (Exception e){
            log.error("[RoleController] --> updateRole: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result newPermission(RoleResourceVo roleResourceVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return roleResourceService.updateResourceByRoleId(roleResourceVo);
        }catch (Exception e){
            log.error("RoleController --> newPermission: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

}

